package com.yanjiali.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanjiali.entity.ArticleLike;
import com.yanjiali.mapper.ArticleLikeMapper;
import com.yanjiali.model.R;
import com.yanjiali.service.ArticleLikeService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
* @Package: com.yanjiali.service.impl
* @Author: yanjiali
* @Created: 2025/4/25 23:34
*/
@Service
public class ArticleLikeServiceImpl extends ServiceImpl<ArticleLikeMapper, ArticleLike> implements ArticleLikeService {

    @Override
    public R<Boolean> isLikedArticle(Long articleId) {
        //获取登录用户信息
        String userId = "";
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null) {
            userId = authentication.getPrincipal().toString();
        }else {
            throw new RuntimeException("用户未登录，无法点赞");
        }

        LambdaQueryWrapper<ArticleLike> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ArticleLike::getUserId, userId);
        lambdaQueryWrapper.eq(articleId != null, ArticleLike::getArticleId, articleId);

        ArticleLike articleLike = getOne(lambdaQueryWrapper);
        if(articleLike == null) {
            return R.ok(false);
        }

        return R.ok(true);
    }
}
